from widget import BasicContainer
import view

class DynamicContainer(BasicContainer):

    def __init__(self, child=None, *args, **kwargs):
        BasicContainer.__init__(self, child, *args, **kwargs)

        self._dx = 0
        self._dy = 0


    def add_child(self, child):
        if self.xstick == 'left':
            child.left = self.left

        else:
            child.right = self.right
        
        if self.ystick == 'bottom':
            child.bottom = self.bottom

        else:
            child.top = self.top
        BasicContainer.add_child(self, child)


    def move_child(self, dx, dy):
        
        self._dx += dx
        self._dy += dy

        if self._dx >= 1 or self._dx <= -1:
            dx = int(self._dx)
            self._dx -= dx
        else:
            dx = 0

        if self._dy >= 1 or self._dy <= -1:
            dy = int(self._dy)
            self._dy -= dy
        else:
            dy = 0


        child = self.child
        if child.width > self.width:

            if child.left + dx >= self.left:
                child.left = self.left

            elif child.right + dx <= self.right:
                child.right = self.right

            else:
                child.move_x(dx)


        elif child.width < self.width:
            
            if child.left + dx <= self.left:
                child.left = self.left

            elif child.right + dx >= self.right:
                child.right = self.right

            else:
                child.move_x(dx)


        elif child.width == self.width:

            if child.left != self.left:
                child.left = self.left



        if child.height > self.height:

            if child.bottom + dy >= self.bottom:
                child.bottom = self.bottom

            elif child.top + dy <= self.top:
                child.top = self.top

            else:
                child.move_y(dy)


        elif child.height < self.height:

            if child.bottom + dy <= self.bottom:
                child.bottom = self.bottom

            elif child.top + dy >= self.top:
                child.top = self.top

            else:
                child.move_y(dy)


        elif child.height == self.height:

            if child.bottom != self.bottom:
                child.bottom = self.bottom



class StaticContainer(BasicContainer):

    def __init__(self, margin_left=5, margin_top=5, margin_right=5, 
                 margin_bottom=5, *args, **kwargs):
        
        self._margin_left = margin_left
        self._margin_right = margin_right
        self._margin_bottom = margin_bottom
        self._margin_top = margin_top

        self.inner_rect = DynamicContainer()
        BasicContainer.__init__(self, *args, **kwargs)
        BasicContainer.add_child(self, self.inner_rect)

        width = (self.right - self._margin_right) - (self.left + self._margin_left)
        if width < 0:
            width = 0
        self.inner_rect.width = width


        if self.xstick == 'left':
            self.inner_rect.left = self.left + self._margin_left
        else:
            self.inner_rect.right = self.right - self._margin_right


        height = (self.top - self._margin_top) - (self.bottom + self._margin_bottom)
        if height < 0:
            height = 0
        self.inner_rect.height = height

        if self.ystick == 'bottom':
            self.inner_rect.bottom = self.bottom + self._margin_bottom
        else:
            self.inner_rect.top = self.top - self._margin_top


    def add_child(self, child):
        self.inner_rect.add_child(child)

        if self.ystick == 'top':
            child.top = self.inner_rect.top

        elif self.ystick == 'bottom':
            child.bottom = self.inner_rect.bottom


        if self.xstick == 'right':
            child.right = self.inner_rect.right

        elif self.xstick == 'left':
            child.left = self.inner_rect.left


    def remove_child(self):
        self.inner_rect.remove_child()


    def get_ystick(self):
        return BasicContainer.get_ystick(self)

    def set_ystick(self, stick):
        BasicContainer.set_ystick(self, stick)
        self.inner_rect.set_ystick(stick)
    ystick = property(get_ystick, set_ystick)


    def get_xstick(self):
        return BasicContainer.get_xstick(self)

    def set_xstick(self, stick):
        BasicContainer.set_xstick(self, stick)
        self.inner_rect.set_xstick(stick)
    xstick = property(get_xstick, set_xstick)


    def _resize_routine(self):
        width = self.width - self._margin_left - self._margin_right
        height = self.height - self._margin_top - self._margin_bottom

        if width < 0:
            width = 0

        if height < 0:
            height = 0

        self.inner_rect.width = width
        self.inner_rect.height = height

        if self.inner_rect.xstick == 'left':
            self.inner_rect.left = self.left + self._margin_left

        elif self.inner_rect.xstick == 'right':
            self.inner_rect.right = self.right - self._margin_right


        if self.inner_rect.ystick == 'top':
            self.inner_rect.top = self.top - self._margin_top

        elif self._inner_rect.ystick == 'bottom':
            self.inner_rect.bottom = self.bottom + self._margin_bottom




class Jacket(StaticContainer):

    def __init__(self, child, *args, **kwargs):
        StaticContainer.__init__(self, child=child,
                                 *args, **kwargs)

        self.on_resize(child.width + self._margin_left + self._margin_right,
                       child.height + self._margin_top + self._margin_bottom)
